package com.clkj.sunhelp.mvp.register;

import android.os.Bundle;
import android.widget.TextView;

import com.clkj.sunhelp.R;
import com.clkj.sunhelp.app.CustomApplication;
import com.clkj.sunhelp.mvp.base.BaseActivity;
import com.clkj.sunhelp.util.CommonUtil;
import com.clkj.sunhelp.util.ToastUtil;
import com.clkj.sunhelp.util.schedulers.SchedulerProvider;
import com.clkj.sunhelp.widget.LineEditText;
import com.jakewharton.rxbinding.view.RxView;

import rx.functions.Action1;
import rx.functions.Func1;

/**注册界面
 * Created by Administrator on 2017/3/27.
 */

public class ActivityRegister extends BaseActivity implements RegisterContract.View {

    private LineEditText etUsername;
    private LineEditText etPass;
    private LineEditText etRealName;
    private LineEditText etContactTel;
    private TextView registertv;


    RegisterContract.Presenter mPresenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initTitleWithRightTvOrIv(null, null, "普通用户注册", true, null, null);
        initData();
        assignView();
        initView();
        mPresenter = new RegisterPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void initData() {

    }

    @Override
    public void assignView() {
        etUsername = (LineEditText) findViewById(R.id.et_username);
        etPass = (LineEditText) findViewById(R.id.et_pass);
        etRealName = (LineEditText) findViewById(R.id.et_real_name);
        etContactTel = (LineEditText) findViewById(R.id.et_contact_tel);
        registertv = (TextView) findViewById(R.id.registertv);
    }

    @Override
    public void initView() {
        RxView.clicks(registertv)
                .filter(new Func1<Void, Boolean>() {
                    @Override
                    public Boolean call(Void aVoid) {
                        boolean result = false;
                        if (CommonUtil.isStringEmpty(etUsername.getText().toString(), "请输入用户名") == false
                                && CommonUtil.isStringEmpty(etPass.getText().toString(), "请输入密码") == false
                                && CommonUtil.isStringEmpty(etRealName.getText().toString(), "请输入您的真实姓名") == false
                                && CommonUtil.isStringEmpty(etContactTel.getText().toString(), "请输入您的联系方式") == false )
                        {
                            result = true;
                        }
                        return result;
                    }
                }).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                register();
            }
        });
    }

    @Override
    public void register() {
        mPresenter.register(etUsername.getText().toString(),
                etPass.getText().toString(),etRealName.getText().toString(),
                etContactTel.getText().toString());
    }

    @Override
    public void onRegisterSuccess() {
        ToastUtil.showShort(this,"注册成功");
        finish();
    }

    @Override
    public void onRegisterError(String tip) {
        ToastUtil.showShort(this,tip);
    }

    @Override
    public void setPresenter(RegisterContract.Presenter presenter) {

    }

    @Override
    public void showLoading() {
        showProgressDialog(false,null);
    }

    @Override
    public void dismissLoading() {
        dismissProgressDialog();
    }
}
